import re
import yaml
import importlib
import hashlib

class Example:

    def getname(self):
        con = '我的名字叫小花'
        print(con)
        return con

    def prt(self,content):
        con = f'我要的打印{content}'
        print(con)
        return con

    def blackball(self,where,who):
        con = f'我要去打篮球，在{where}，跟{who}'
        print(con)
        return con
    
    def add(self,p1,p2):
        print(type(p1))
        return p1+p2
    
    # 1585b265ad4cff61e091d9d1e6cda5a7
    def md5_encode(self,data):

    #把data转化为utf-8的编码格式
        data = str(data).encode('utf-8')

    #md5加密

        md5_value = hashlib.md5(data).hexdigest()
        print(md5_value)
        return md5_value.upper()


#没有用处的代码
def create_object(module_name, class_name, **kwargs):
    module = importlib.import_module(module_name)
    cls = getattr(module, class_name)
    return cls(**kwargs)

# 根据字符串动态创建对象
# circle = create_object("shapes", "Circle", radius=5)
# square = create_object("shapes", "Square", side=10)

# print(circle.radius)  # 输出: 5
# print(square.side)    # 输出: 10
def subFunc(match):
    sub,func_name,param = match.group(1),match.group(2),match.group(3)
    # print('*****kaishi*******')
    # print(sub)
    # print('****')
    # print(func_name)
    # print('*****')
    # print(param)
        
    if not param :
        return getattr(Example(),func_name)()
    else:
        param_list = param.split(',')
        # print(param_list)
        return getattr(Example(),func_name)(*param_list)



def hot_load():
    with open('6_requests/jichu/example4.yaml') as f:
        data = yaml.safe_load(f)

    partten = "(\$\{(.*?)\((.*?)\)\})"

    res = re.findall(partten,yaml.safe_dump(data))
    # print(res)
    
    # sub自带循环，对每一个匹配上的partten都进行subFunc处理
    new_data = re.sub(partten,subFunc,yaml.safe_dump(data))
    print(new_data)
    # 创建对象
    # for class_name in data.keys():
        # print(class_name)



    # for func_name,param in res:
    #     if not param :
    #         getattr(Example(),func_name)()
    #     else:
    #         param_list = param.split(',')
    #         print(param_list)
    #         getattr(Example(),func_name)(*param_list)
            

hot_load()
